package com.das.mechanic_alone.widget;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.alone.X3AloneShowNumAdapter;
import com.das.mechanic_base.app.X3MyApp;
import com.das.mechanic_base.base.X3BaseSelfBottomSheetDialog;
import com.das.mechanic_base.bean.alone.AloneNewBean;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.widget.X3AlonePassInstructionDialog;
import com.das.mechanic_base.widget.X3MemberButtonView;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

public class X3BottomAloneStatusDialog extends X3BaseSelfBottomSheetDialog implements X3AloneShowNumAdapter.IOnClickJump {

    RecyclerView rlv_answer;

    TextView tv_num;
    TextView tv_pass_all;
    ImageView iv_introduction;

    X3MemberButtonView btn_submit;

    private X3AloneShowNumAdapter showNumAdapter;

    public X3BottomAloneStatusDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        rlv_answer = getView(R.id.rlv_answer);
        btn_submit = getView(R.id.btn_submit);
        tv_num = getView(R.id.tv_num);
        iv_introduction = getView(R.id.iv_introduction);
        tv_pass_all = getView(R.id.tv_pass_all);
        rlv_answer.setLayoutManager(new LinearLayoutManager(mContext));
        showNumAdapter = new X3AloneShowNumAdapter(mContext);
        rlv_answer.setAdapter(showNumAdapter);
        showNumAdapter.setiOnClickJump(this);

        tv_num.setOnClickListener(view -> dismiss());
        btn_submit.setiOnClickAffirm(() -> {
            dismiss();
            if (iOnClickJump != null) {
                iOnClickJump.iOnSubmitAlone();
            }
        });

        tv_pass_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (iOnClickJump != null) {
                    iOnClickJump.iOnClickPass(1,-1,-1);
                }
            }
        });

        iv_introduction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                X3AlonePassInstructionDialog x3AlonePassInstructionDialog = new X3AlonePassInstructionDialog(X3MyApp.getInstance().getTopActivity());
                x3AlonePassInstructionDialog.show();
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_alone_status_service;
    }

    @Override
    protected int getDialogHeight() {
        return (int) (X3ScreenUtils.getScreenHeight(mContext) * 0.9);
    }

    public void changeData(List<List<String>> isStatus, List<AloneNewBean.ItemQuestionListBean> aloneNewBeans,List<List<String>> contentList,
                           int allNum,int aleadyAnswerNum,String submitText,boolean status){
        if (showNumAdapter != null) {
            showNumAdapter.changeData(isStatus,aloneNewBeans,contentList);
        }

        String aleady = aleadyAnswerNum + "";
        String all = "/" + allNum;

        SpannableString numSpan = new SpannableString(aleady + all);
        numSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#0077ff")), 0, aleady.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        numSpan.setSpan(new RelativeSizeSpan(0.86f), aleady.length(), aleady.length() + all.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        numSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#B1B3BD")), aleady.length(), aleady.length() + all.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tv_num.setText(numSpan);

        btn_submit.changeBtnStatus(status);
        btn_submit.changeText(submitText + "");
    }


    @Override
    public void iOnClickJumpToIndex(int titleIndex, int index) {
        dismiss();
        if (iOnClickJump != null) {
            iOnClickJump.iOnClickJumpToIndex(titleIndex,index);
        }
    }

    @Override
    public void iOnClickItemPass(int titleIndex, int index) {
        if (iOnClickJump != null) {
            iOnClickJump.iOnClickPass(2,titleIndex,index);
        }
    }

    IOnClickJump iOnClickJump;

    public void setiOnClickJump(IOnClickJump iOnClickJump) {
        this.iOnClickJump = iOnClickJump;
    }

    public interface IOnClickJump {
        void iOnClickJumpToIndex(int titleIndex,int index);

        void iOnSubmitAlone();
        void iOnClickPass(int type,int titleIndex,int index);
    }

    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("施工单详情状态弹窗");
    }

    @Override
    public void dismiss() {
        super.dismiss();
        MobclickAgent.onPageEnd("施工单详情状态弹窗");
    }
}
